package com.wcx.application.controller;

import com.wcx.application.domian.BossBean;
import com.wcx.application.domian.JobBean;
import com.wcx.application.domian.JobDetailBean;
import com.wcx.application.domian.JobTechBean;
import com.wcx.application.service.BossService;
import com.wcx.application.service.JobDetailService;
import com.wcx.application.service.JobService;
import com.wcx.application.utils.Result;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;

@RestController
@RequestMapping("/job")
@CrossOrigin
public class JobController {

    @Autowired
    JobService jobService;

    @Autowired
    JobDetailService jobDetailService;

    @GetMapping("/list")
    public Result getList() {
        System.out.println("---" + jobService.getList());
        List<JobBean> list = jobService.getList();
        for (JobBean jobBean : list) {
            List<String> techName = jobService.selectTech(jobBean.getId());
            jobBean.setTechnologyName(techName);
        }
        return Result.ok().data("list",list);
    }

    @PostMapping("/update")
    public Result update(@RequestBody JobBean jobBean) {
        int i = jobService.update(jobBean);
        int i1 = jobService.deleteTech(jobBean.getId());
        if (jobBean.getTechnologyId() == null || jobBean.getTechnologyId().size() == 0) {
            // 处理空数组的情况
            if (i != 0 && i1!=0 ) {
                return Result.ok();
            } else {
                return Result.ok();
            }
        } else {
            // 数组非空，继续执行插入操作
            JobTechBean jobTechBean =new JobTechBean();
            jobTechBean.setJobId(jobBean.getId());
            jobTechBean.setTechnologyName(jobBean.getTechnologyId());
            int   tech = jobService.createTech(jobTechBean);
            if (i != 0 && i1!=0 && tech!=0) {
                return Result.ok();
            } else {
                return Result.ok();
            }
        }
//
//        if (i != 0 && i1!=0 && tech!=0) {
//            return Result.ok();
//        } else {
//            return Result.error();
//        }
    }

    @PostMapping("/search")
    public Result search(@RequestBody JobBean jobBean) {
        System.out.println("---123"+jobBean.toString());
        List<JobBean> jobBeans = jobService.searchList(jobBean);
        for (JobBean jobBean1 : jobBeans) {
            List<String> techName = jobService.selectTech(jobBean1.getId());
            jobBean1.setTechnologyName(techName);
        }
        return Result.ok().data("list", jobBeans);
    }

    @PostMapping("/create")
    public Result create(@RequestBody JobBean jobBean) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("YYYY年MM月dd日 HH:mm:ss");
        Calendar calendar = Calendar.getInstance();
        String time = simpleDateFormat.format(calendar.getTime());
        jobBean.setCreateTime(time);
        int tech=0;
        int i = jobService.create(jobBean);
        System.out.println("-----------"+i);
        System.out.println("-----------"+jobBean.getId());
        JobTechBean jobTechBean =new JobTechBean();
        jobTechBean.setJobId(jobBean.getId());
        jobTechBean.setTechnologyName(jobBean.getTechnologyId());
        JobDetailBean jobDetailBean = new JobDetailBean();
        jobDetailBean.setJobId(jobBean.getId());
        System.out.println(jobDetailBean.getJobId());
        jobDetailService.create(jobDetailBean);
        if (jobTechBean.getTechnologyName() != null) {
           tech = jobService.createTech(jobTechBean);
        }
        if (i != 0) {
            return Result.ok().data("id",jobBean.getId());
        } else {
            return Result.error();
        }

    }

    @DeleteMapping("/delete")
    public Result delete(int id) {
        int i = jobService.delete(id);
        int i1 = jobService.deleteTech(id);
        int i2 = jobDetailService.delete(id);
        if (i != 0) {
            return Result.ok();
        } else {
            return Result.error();
        }
    }


}